package scm.erp.sale.plan.service;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import scm.erp.sale.plan.domain.Plan;
import scm.erp.sale.plan.mapper.PlanMapper;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class PlanService {
    private final PlanMapper planmapper;
    public PlanService(PlanMapper mapper) {this.planmapper = mapper;}

    /**
     * 全部查询
     * @param searchVo
     * @return
     */
    public List<Plan.Entity> list(Plan.SearchVo searchVo) {
        List<Plan.Entity> entityList = this.planmapper.select(searchVo);
        log.info("查询数量:{}", entityList.size());
        return entityList;
    }
    /**
     * 查询客户
     * @param searchCustomerNameVo
     * @return
     */
    public List<Plan.searchCustomerNameVo> searchCustomerName(Plan.searchCustomerNameVo searchCustomerNameVo) {
        List<Plan.searchCustomerNameVo> CustomerNameVoList = planmapper.searchCustomerName(searchCustomerNameVo);
        return CustomerNameVoList;
    }

    /**
     * 查询货物
     * @param searchFreightNameVo
     * @return
     */
    public List<Plan.searchFreightNameVo> searchFreightNameVo(Plan.searchFreightNameVo searchFreightNameVo) {
        List<Plan.searchFreightNameVo> FreightNameVoList = planmapper.searchFreightName(searchFreightNameVo);
        return FreightNameVoList;
    }

    /**
     * 查询计划编码
     * @param searchVo
     * @return
     */
    public String searchPlanCode(Plan.searchPlanCodeVo searchVo) {
        return searchVo.getPlanCode();
    }
    /**
     * 新增
     * @param vo
     * @return
     */
    public String insert(Plan.Vo vo) {
        log.info("准备新增计划...");
        int ret = planmapper.insert(vo);
        log.info("新增计划结果:{}", ret);
        return vo.getId();
    }

    /**
     * 更新
     * @param vo
     * @return
     */
    public Boolean update(Plan.Vo vo) {
        log.info("准备更新计划...");
        int updateNum = planmapper.update(vo);
        log.info("更新计划结果:{}", updateNum);
        return updateNum == 1;
    }

    public Plan.Vo one(String id) {
//        Plan.Entity one = this.planmapper.selectAll(id);
        Plan.Entity one = this.planmapper.selectById(id);
        if (one == null) {
            log.warn("计划不存在!");
            return null;
        }
        return one.convert(Plan.Vo.class);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void batchRemove(List<String> ids) {
        int idList = this.planmapper
                .removeByIds(ids);
        if (idList  > 0) {
            log.info("删除计划结果:{}", idList);
            }
        }


    /**
     * remove
     * @param id
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void remove(String id) {
        int deleteNum = this.planmapper.remove(id);
        log.info("删除计划结果:{}", deleteNum);
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void delete(String id) {
        int deleteNum = this.planmapper.delete(id);
        deleteNum = this.planmapper.delete(id);
        log.info("逻辑删除品类结果:{}", deleteNum);
        }
    /**
     * 物理批量删除
     *
     * @param ids 品系ID
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void deleteByIds(List<String> ids) {
        int idList = this.planmapper
                .deleteByIds(ids);
        if (idList  > 0) {
            log.info("删除计划结果:{}", idList);
        }
    }

    /**
     * 导出
     * @param vo
     * @param response
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void export(Plan.SearchVo vo, HttpServletResponse response) {
        List<Plan.Entity> seriesList = this.planmapper.select(vo);
    }

    /**
     * 批量新增
     * @param insertVoList
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<String> insertBatch(List<Plan.Vo> insertVoList) {
        return insertVoList
                .stream()
                .map(this::insert)
                .collect(Collectors.toList());
    }

    /**
     * 启用
     * @param id
     * @return
     */
    public int updateStatusEnable(String id) {
        int updateStatus = this.planmapper.updatePlanStatusEnable(id);
        log.info("更新状态返回结果:{}", updateStatus);
        return updateStatus;
    }

    /**
     * 禁用
     * @param id
     * @return
     */
    public int updateStatusDisable(String id) {
        int updateStatus = this.planmapper.updatePlanStatusDisable(id);
        log.info("更新状态返回结果:{}", updateStatus);
        return updateStatus;
    }


}

